package com.spboot.app.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spboot.app.mapper.BaomingMapper;
import com.spboot.app.pojo.Baoming;
import com.spboot.app.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class BaomingService {

    @Autowired
    private BaomingMapper mapper;

    // 报名赛事
    public R<Object> insert(Baoming baoming, Map data) {
        // 检查是否已报名
        QueryWrapper<Baoming> wrapper = new QueryWrapper<>();
        wrapper.eq("saishi_id", baoming.getSaishiId())
                .eq("yonghu_id", baoming.getYonghuId());
        if (mapper.selectCount(wrapper) > 0) {
            return R.error("您已报名该赛事");
        }

        // 插入报名记录
        mapper.insert(baoming);
        return R.success("报名成功");
    }

    public R<List<Map<String, Object>>> getListBySaishiId(Integer saishiId) {
        return R.success(mapper.getListBySaishiIdWithUser(saishiId));
    }

    public R<Object> delete(Baoming baoming, Map data) {
        QueryWrapper<Baoming> wrapper = new QueryWrapper<>();
        wrapper.eq("saishi_id", baoming.getSaishiId())
                .eq("yonghu_id", baoming.getYonghuId());
        Baoming record = mapper.selectOne(wrapper);
        if (record == null) {
            return R.error("您尚未报名该赛事");
        }

        mapper.delete(wrapper); // 删除报名记录
        return R.success("取消成功");
    }


}
